Implement a C++ program to demonstrate virtual functions and pure virtual functions
It includes explanations, code, and output, suitable for educational blogs, tutorials, or content writing.
🌟 Understanding Virtual and Pure Virtual Functions in C++
In object-oriented programming, polymorphism allows objects to behave differently based on their context. In C++, virtual functions and pure virtual functions are key tools for implementing polymorphism, especially with inheritance. Let’s explore these concepts with a simple and practical example.
✅ What is a Virtual Function?
A virtual function is a member function in the base class that can be overridden in a derived class. It allows runtime polymorphism, meaning the decision about which function to call is made at runtime.
✅ What is a Pure Virtual Function?
A pure virtual function is a virtual function that is declared but not defined in the base class. It forces all derived classes to provide their own implementation. A class containing at least one pure virtual function becomes an abstract class, and objects of abstract classes cannot be created.
🧑💻 C++ Program: Virtual & Pure Virtual Function Example
#include <iostream>
using namespace std;
// Abstract base class
class Shape {
public:
// Virtual function
virtual void display() {
cout << "This is a shape." << endl;
}
// Pure virtual function
virtual float area() = 0;
};
// Derived class 1
class Circle : public Shape {
private:
float radius;
public:
Circle(float r) {
radius = r;
}
void display() override {
cout << "This is a circle." << endl;
}
float area() override {
return 3.14 * radius * radius;
}
};
// Derived class 2
class Rectangle : public Shape {
private:
float length, width;
public:
Rectangle(float l, float w) {
length = l;
width = w;
}
void display() override {
cout << "This is a rectangle." << endl;
}
float area() override {
return length * width;
}
};
int main() {
Shape* shapePtr;
Circle c(5.0);
Rectangle r(4.0, 6.0);
// Virtual function call using base class pointer
shapePtr = &c;
shapePtr->display();
cout << "Area: " << shapePtr->area() << endl;
shapePtr = &r;
shapePtr->display();
cout << "Area: " << shapePtr->area() << endl;
return 0;
}
🖨️ Output
This is a circle.
Area: 78.5
This is a rectangle.
Area: 24
🧠Explanation
1. The class `Shape` is an abstract base class with a virtual function `display()` and a pure virtual function `area()`.
2. The derived classes `Circle` and `Rectangle` override both functions.
3. The base class pointer `shapePtr` is used to invoke the correct version of functions at runtime — enabling polymorphism.
💡 Key Concepts Covered
- Virtual functions allow a base class pointer to invoke derived class behavior.
- Pure virtual functions force derived classes to define their own version.
- Abstract classes are blueprints that cannot be instantiated directly.
🎯 Conclusion
Using virtual and pure virtual functions in C++ is an essential practice for designing flexible, extensible, and polymorphic class hierarchies. It forms the backbone of runtime polymorphism, enabling dynamic behavior based on the actual object type during program execution.
Comments
Post a Comment